<?php
			ob_start();			
?>
<?php
		
		if (isset($_COOKIE["user"])) {
			// if cookie <> ""
		  }
		  else
		  {
			  header("location:login.php");
			  
		  }
?>
<?php
			//select value from DB to show on textbox
			@$pro_id = $_REQUEST["pro_id"];		
?>
<?php
function deleteRecord($id_imagename)
{
	//get id from array
	$gal_id = key($id_imagename);
	//get cat_image from array
	$imagename = current($id_imagename);
	$sql = "DELETE FROM tbl_gallery WHERE gal_id={$gal_id}";
	$result = query($sql);	
	if($result)
	{
		deleteFile("gallery/{$imagename}");
		// delete small gallery
		$imagename=$imagename."-s.jpg";
		deleteFile("gallery/{$imagename}");
		
		errorMessage("Supprimer réussies!");
		
		//pageRedirect("galleryNew.php","");
	}	
	else
	{
		errorMessage("Supprimer échec ..!");
	}	
}
?>
<?php
	if(@is_array($_REQUEST["gal_id"]))
{
	$id_imagename = $_REQUEST["gal_id"];
	deleteRecord($id_imagename);
}
?>
<?php
	if(isset($_REQUEST["pro_id"]))
	{
		
		
		$sql = "SELECT * FROM tbl_product WHERE pro_id={$pro_id} LIMIT 1";
		$result = query($sql);
		$row = fetch_assoc($result);
		
		$pro_id=$row["pro_id"];
		$pro_name=$row["pro_name"];
		$published=$row["published"];				
	}
?>
<?php
                    if(isset($_POST["btnsubmit"]))
                    {
						$uploadfile = escapeValue($_FILES["uploadfile"]["name"]);
						$product=escapeValue($_POST["cboproduct"]);
						$description=escapeValue($_POST["txtdescription"]);
						
							if(@$errors == "")
								{
									if($uploadfile=="")
									{
										errorMessage("S'il vous plaît choisir l'image à charger ...!");	
									}
									elseif($product=="")
									{
										errorMessage("S'il vous plaît sélectionner un produit ...!");	
									}
									else
									{
										//uploadGallery("uploadfile");
										
										//Upload Image
										$image_name = $_FILES["uploadfile"]["name"];
										$image_tmp_name = $_FILES["uploadfile"]["tmp_name"];
										$ext = "jpg";	
										$tag = "";
										$path = "gallery";
										
										// pop up Photo	and riginal image		
										list($width, $height) = getimagesize($image_tmp_name);			
										createImageAndThumb($image_tmp_name,$image_name,
															$width,
															$height,
															$tag,
															$path,
															$image_name);
										// show small Photo
										$filename_s = "$uploadfile-s.$ext";	
										$width = 78;
										$height = 93;			
										createImageAndThumb($image_tmp_name,$filename_s,
															$width,
															$height,
															$tag,
															$path,
															$image_name);
										// end upload photo
										
										if(@$errors == "")
										{
											$sql = "INSERT INTO tbl_gallery( pro_id, description
											,photo) 
											VALUES('$product','$description','$uploadfile')";
											if(query($sql))
											{
												errorMessage("Inséré avec succès ...!");
												//clear data
												$uploadfile = "";
												//$prodcut="";
												$description="";
											}
												
										}
									}			
													
								}//end check error=""
							
                    } //end isset btnsubmit
                ?>
					<div id="border_cont_title">
                        	<div id='content_title_left'>gestion galerie</div>
                           <div id='content_title_right'></div>
					</div> <!--end border_cont_title-->
                    <?php echo @$errors;?>
                     <!--start table -->
                     <?php
					 	if($published==1)
						{
					 ?>
                     		<form action="index.php?page=galleryNew&pro_id=<?php echo $pro_id;?>" method="post" enctype="multipart/form-data">
                    	<table width="765px" align="left">
                        	<tr>
                            	
                            	<td align="left" height="30" bgcolor="#FFEAD7">Envoyer la photo*</td>
                                <td>
                                	<input class="submit" type="file" name="uploadfile" />
                                </td>
                                
                            </tr>
                                <input type="hidden" name="cboproduct" value="<?php echo @$pro_id; ?>"  />
                            <tr>
                            	<td align="left" height="30" bgcolor="#FFEAD7">Description</td>
                                <td>
                                <textarea name="txtdescription" cols="30" rows="5" wrap="virtual"><?php echo @$description; ?></textarea>
                                </td>
                            </tr>
                            
                            <tr>
                            	<td align="left" height="30" bgcolor="#FFEAD7">Action</td>
                                <td align="left" height="30">
                                <input class="submit" type="submit" name="btnsubmit" value="Soumettre"  />
                                <input class="submit" type="button" name="btncancel" value="Annuler" onclick="redirect('index.php?page=product')" />

                                </td>
                            </tr>
                            
                        </table>
                        </form>
                     <?php
						}
						else
						{
					 		//errorMessage("Please publish product...!");
							echo "<div style='font-weight:bold;color:#F09;margin-bottom:5px;'> S'il vous plaît publier produit ...!</div>";
						}
                     ?>
                     
                        <!-- start gallery-->
                    	<table width="770px">
                            	<tr>
                                    <th>Photo</th>
                                    <th>Produit</th>
                                    <th>Description</th>
                                    <th>Action</th>
                                </tr>
                             
                                <?php
								//include function from cls_pag
								include('../includes/cls_pagination.php');
								
                                    $sql = "SELECT * from  v_gallery WHERE pro_id='$pro_id'"; 
                                    $result = query($sql);
									$Num_Rows = mysql_num_rows($result);
							
									$Per_Page = 6;   // Per Page
									
									@$Page = $_GET["Page"];
									if(!@$Page)
									{
										$Page=1;
									}
									
									$Prev_Page = $Page-1;
									$Next_Page = $Page+1;
									
									$Page_Start = (($Per_Page*$Page)-$Per_Page);
									if($Num_Rows<=$Per_Page)
									{
										$Num_Pages =1;
									}
									else if(($Num_Rows % $Per_Page)==0)
									{
										$Num_Pages =($Num_Rows/$Per_Page) ;
									}
									else
									{
										$Num_Pages =($Num_Rows/$Per_Page)+1;
										$Num_Pages = (int)$Num_Pages;
									}
									
									$sql .=" ORDER BY gal_id DESC  LIMIT $Page_Start , $Per_Page";
									$result  = mysql_query($sql);
                                    while($row = fetch_assoc($result))
                                    {
                                ?>
                                <tr>
                                	<td align="center">
                                    <img src="gallery/<?php echo $row["photo"] ;?>" width="80px" height="70px" />
                                    </td>
                                    <td align="center"><?php echo $row["product"];?></td>
                                    <td align="center"><?php echo $row["description"];?></td>
                                    <td align="center">
                                   <!--<a href="galleryEdit.php?gal_id=<?php echo $row["gal_id"]?>"><img class="action_distant" src="images/edit.png"  /></a>-->
                                   <a href="index.php?page=galleryNew&gal_id[<?php echo $row["gal_id"];?>]=<?php echo $row["photo"];?>&pro_id=<?php echo $row["pro_id"];?>" onclick="return confirm('Êtes-vous sûr de vouloir supprimer cette galerie?');"><img class="action_distant" src="images/delete.png" /></a>
                                    </td>
                                </tr>
                                <?php
									}//end while

                                ?>
                            </table>
                         <!--end table -->
                    <?php
								echo "<div id='Paginator_left'> Total $Num_Rows Record ";
								
                                
                                    $pages = new Paginator;
                                    $pages->items_total = $Num_Rows;
                                    $pages->mid_range = 6;
                                    $pages->current_page = $Page;
                                    $pages->default_ipp = $Per_Page;
                                    //$pages->url_next = $_SERVER["PHP_SELF"]."?QueryString=value&Page=";
									$pages->url_next ="index.php?page=galleryNew&pro_id=$pro_id&QueryString=value&Page=";
                                    $pages->paginate();
                                    
                                    echo $pages->display_pages();
									
									
                               echo " </div>" ;
						?>
						

